For IMAP there does not appear to be a way of reading the headers only.
For POP3, you can choose to "Fetch Headers Only" which will mean that the body of the email remains on the server until you explicitly request it to be downloaded.
For filtering, you can create rules based on any header you like. Go to
Tools / Message Filters... / New...
Choose a name for the filter, then in the filter criteria dropdown box (which defaults to Subject) choose Customise...
Here you can add a custom message header that can be used in filters. For example, you can add
X-Spam-Flag
And close the dialog, then this will appear as a filter option in the dropdown. Thunderbird will look for the header in emails when processing filters, and if it matches the criteria, will carry out the filter action.